#import "BWPartialStitchWaterdropTool.h"
#import "BWCrossStitchChangeList.h"

#import "BWCrossStitchLayer.h"
#import "BWCrossStitchSet.h"

@implementation BWPartialStitchWaterdropTool

// right now only adds a partial stitch if one corner has one color, and
// the opposite coner has a different color
// if colors the same, make a full stitch
// if colors differ, make a partial stitch

/* test cases:  [. is where a stitch should be added]
 *                     X X
 *   .X       .XX.    .X X.
 *   X.       X..X    XXXXX
 *
 *   .X O.   .X     .XXXX
 *   X. .O   X:O    X .O.
 *            O.    .XOOO
 *
 *   X
 *   .O
 *   OO
 */

- (void) addChangeAtRow: (int) row
                 column: (int) column
{
    // get what's around the current point
    BWCrossStitchSet *set = [layer crossStitches];
    BWCrossStitch *up, *down, *left, *right;

    [set getSymbol: &up     atRow: row - 1  column: column];
    [set getSymbol: &down   atRow: row + 1  column: column];
    [set getSymbol: &left   atRow: row      column: column - 1];
    [set getSymbol: &right  atRow: row      column: column + 1];

    BWThread *topThread = nil, *bottomThread = nil;
    BWThread_StitchDivider divider = kDivider_None;

    // check for two-and-two's first
    if ([up topThread] == [left topThread]) {
        if ([down topThread] == [right topThread]) {
            topThread = [up topThread];
            bottomThread = [right topThread];
            if (topThread != bottomThread) {
                divider = kDivider_Forward;
            }
        }
    }
    if ([up topThread] == [right topThread]) {
        if ([down topThread] == [left  topThread]) {
            topThread = [up topThread];
            bottomThread = [left topThread];
            if (topThread != bottomThread) {
                divider = kDivider_Backward;
            }
        }
    }
    if (topThread == nil && bottomThread == nil) {

        // try for two-and-ones
        // top-left
        if ([up topThread] == [left topThread]) {
            if ([up topThread] != [down topThread]
                && [up topThread] != [right topThread]) {
                topThread = [up topThread];
                bottomThread = nil;
                divider = kDivider_Forward;
            }
        }

        // top-right
        if ([up topThread] == [right topThread]) {
            if ([up topThread] != [down topThread]
                && [up topThread] != [left topThread]) {
                topThread = [up topThread];
                bottomThread = nil;
                divider = kDivider_Backward;
            }
        }

        // bottom-left
        if ([down topThread] == [left topThread]) {
            if ([down topThread] != [up topThread]
                && [down topThread] != [right topThread]) {
                topThread = nil;
                bottomThread = [down topThread];
                divider = kDivider_Backward;
            }
        }

        // bottom-right
        if ([down topThread] == [right topThread]) {
            if ([down topThread] != [up topThread]
                && [down topThread] != [left topThread]) {
                topThread = nil;
                bottomThread = [down topThread];
                divider = kDivider_Forward;
            }
        }
    }

    if (topThread == nil && bottomThread == nil) {
        goto done;
    }

    BWCrossStitch *stitch;
    stitch = [[BWCrossStitch alloc]
                 initWithRow: row
                 column: column
                 topThread: topThread
                 bottomThread: bottomThread
                 divider: divider
                 tag: kCrossStitchChange_add];

    [changeList addStitch: stitch];

    [stitch release];

done:
    return;
    
} // addChangeAtRow


- (void) startDragAtRow: (int) row  
                 column: (int) column
                 thread: (BWThread *) thread
{
    [self addChangeAtRow: row  column: column];

    [self invalidateRow: row  column: column];

} // startDragAtRow


- (void) continueDragAtRow: (int) row
                    column: (int) column
                    thread: (BWThread *) thread
{
    [self addChangeAtRow: row  column: column];

    [self invalidateRow: row  column: column];

} // continueDragAtRow


- (NSString *) name
{
    return (@"Add partial stitches to smooth out curves");
} // name

@end // BWPartialStitchWaterdropTool

